<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script type="text/javascript">
			//10. 和电脑循环玩一个猜拳的小游戏：
			//1. 电脑随机出，用户输入石头、剪刀、布，然后和电脑生成的进行对比，并判断谁赢，然后继续下一轮。
			//2. 如果用户输出：退出，那么程序退出，然后分别打印出电脑、用户赢的次数、用户连续赢的最高次数。
			//3. 用户点击取消：那么直接开始下一轮。
			var arr = ["石头", "剪刀", "布"];
			var player,computer;
			var win = false; //用户连胜次数(刚开始都为0)
			var flag = true;
			var playercount = 0; //用户赢的次数
			var playerwin, playerwin2 = 0; //用户连赢次数
			var computercount = 0; //电脑赢的次数
			while (flag) {
				player = window.prompt("请输入石头或剪刀或布(退出游戏输入退出)：");
				computer = parseInt(Math.random() * 3);

				if (player == "石头" && arr[computer] == "剪刀" || player == "剪刀" && arr[computer] == "布" || player == "布" && arr[computer] == "石头") {
					alert("你赢了");
					playercount += 1;
					if (win) { //赢了一局之后下一局还是赢win已经为true；直接执行这一步
						playerwin += 1;
					} else { //如果赢了就把win改为true，连赢得次数加一
						win = true;
						playerwin += 1;
					}
				} else if (player == arr[computer]) {
					alert("打平了");
				} else if (player == "石头" && arr[computer] == "布" || player == "剪刀" && arr[computer] == "石头" || player == "布" && arr[computer] == "剪刀") {
					alert("电脑赢");
					computercount += 1;//电脑赢了电脑赢得次数加一
					win = false;//用户连赢变为false
					playerwin = 0;//连赢次数中断变为0次
				} else if (player == "退出") {
					flag = false;//为false跳出循环
				}
				if (playerwin > playerwin2) {//连胜次数大于0，则把连胜次数赋给0
					playerwin2 = playerwin;
				}
			}
			document.write("电脑赢的次数：" + computercount);
			document.write("<br />");
			document.write("用户赢的次数：" + playercount);
			document.write("<br />");
			document.write("用户连赢的次数：" + playerwin2);
		</script>
	</body>
</html>
